<template>
  <div>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">发药</i>
    </div>
    <el-divider></el-divider>

    <el-row :gutter="20">
      <el-col :span="6"><el-input v-model="finish_price_search.caseNumber" placeholder="请输入患者病历号"></el-input></el-col>
      <el-col :span="6"><el-input v-model="finish_price_search.realName" placeholder="请输入患者姓名"></el-input></el-col>
      <el-col :span="2"><el-button @click="getFinishPricePatient">搜索</el-button></el-col>
      <el-col :span="10"> </el-col>
    </el-row>
    <el-table style="margin-top:5px;" :data="finish_price_patient" size="mini">
      <el-table-column type="index" label="编号" width="80"></el-table-column>
      <el-table-column prop="realName" label="患者姓名" width="180"></el-table-column>
      <el-table-column prop="caseNumber" label="患者病历号"></el-table-column>
      <el-table-column prop="age" label="患者年龄" width="180"></el-table-column>
      <el-table-column prop="gender" label="患者性别" width="180"></el-table-column>
      <el-table-column width="120">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="selectPatientDrugs(scope.$index,scope.row)">显示药品</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:5px;text-align:right;width:80%">
      <el-pagination
          :default-current-page="currPage"
          :page-size="pageSize"
          :total="totalCount" @current-change="finishCheckPatientChange"
      layout="prev,pager,next">
      </el-pagination>
    </div>
    <el-divider></el-divider>

    <el-table
        v-loading="loading"
        :data="finish_check_patient_select" style="margin-top:20px;" size="mini">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="drugCode" label="药品编码"></el-table-column>
      <el-table-column prop="drugName" label="药品名称"></el-table-column>
      <el-table-column prop="drugFormat" label="药品规格" width="180"></el-table-column>
      <el-table-column prop="drugUnit" label="包装单位" width="110"></el-table-column>
      <el-table-column prop="manufacturer" label="生产厂家" width="200"></el-table-column>
      <el-table-column prop="drugOrice" label="单价" width="120"></el-table-column>
      <el-table-column prop="drugNumber" label="药品数量" width="120"></el-table-column>
      <el-table-column width="120">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="givePatientDrugs(scope.$index,scope.row)">发药</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>
<script >
import qs from 'qs'
import {ElMessage} from "element-plus";
export default {
  data(){
    return {
      //控制页面加载效果 true显示加载，false不显示加载
      loading:false,
      currPage:1, //当前页
      pageSize:5,//每页显示患者数量
      totalCount:0,//总条数
      temp_select_patient:'',//记录最后点击的患者
      finish_price_search:{
        caseNumber:'',
        realName:'',
      },
      update_check_input:{
        check_result:'',
        id:"",
        employee_id:'',
      },
      finish_check_patient_select:[ ],
      finish_price_patient:[]
    }
  },
  methods:{

    //点击当前行，得到患者信息
    selectPatientDrugs:function (index,row){
      this.temp_select_patient=row
      console.log("=======================================")
      console.log(this.temp_select_patient)
      console.log("=======================================")
      this.getPatientDrugInfo()
    },
    //分页事件对应的函数
    finishCheckPatientChange:function (currPage){
      this.currPage=currPage
    },
    //得到已缴费的患者
    getFinishPricePatient:function ()
    {


      let url=`http://localhost:8092/register/selectpatients/${this.currPage}`
      this.$axios.post(url,this.finish_price_search).then(resp=>{
        this.finish_price_patient=resp.data.result
        this.totalCount=resp.data.total
        if(this.finish_price_patient.length==0)
        {
          ElMessage({
            type:'warning',
            message:'没有病历号'
          })
        }


      })
    },
    //查询该患者下的对应的药品信息
    getPatientDrugInfo:function (){
      //
      this.loading=true

      let url=`http://localhost:8091/druginfo/selectbyregisterid/${this.temp_select_patient.id}`
      this.$axios.get(url).then(resp=>{
        //
        this.loading=false
        this.finish_check_patient_select=resp.data.result

        console.log(this.finish_check_patient_select)
        if(this.finish_check_patient_select.length==0)
        {
          ElMessage({
            type:'warning',
            message:'该患者没有药品发放'
          })
        }

      })
    },

    // 修改处方状态
    givePatientDrugs:function (index,row){

      let url='http://localhost:8091/druginfo/updatestate'
      let value={
        id:row.pid,
        drugState:'已发药',
        //挂号的主键id
        registerId:this.temp_select_patient.id
      }
      this.$axios.post(url,value).then(resp=>{

      ElMessage({
        type:'success',
        message:'药品发放成功'
      })
        this.getPatientDrugInfo()
      })

    }
  },
  mounted() {
  }
}
</script>